﻿using Castle.Core.Configuration;
using Inovout.Application;
using System;
using System.ServiceProcess;

namespace Inovout.YunYao.Cattle.WindowsService
{
    public class CattleWindowsServiceApplication : ServiceBase,IApplication
    {
        private ApplicationWrapper applicationWrapper;
        private static CattleWindowsServiceApplication application;
        static void Main(string[] args)
        {
            application = new CattleWindowsServiceApplication();
            application.Run();
        }
        private NodePersistentService nodePersistentService;
        protected override void OnStart(string[] args)
        {
            nodePersistentService = new NodePersistentService(this);
            nodePersistentService.Start();
        }

        protected override void OnStop()
        {
            nodePersistentService.Stop();
        }

        public bool IsDebug
        {
            get
            { 
#if DEBUG
                return false;
#else
                return true;
#endif
            }
        }

        public string Title
        {
            get { return applicationWrapper.Title; }
        }

        public string Name
        {
            get;
            private set;
        }

        public string Path
        {
            get;
            private set;
        }

        public void Run()
        {
            Name = this.GetType().Assembly.GetName().Name;
            Path = AppDomain.CurrentDomain.BaseDirectory;
            applicationWrapper = new ApplicationWrapper(this);
            applicationWrapper.Initialize();


            if (Environment.UserInteractive)
            {
                application.OnStart(null);
                Console.WriteLine("Press any key to stop program");
                Console.Read();
                application.OnStop();
            }
            else
            {
                ServiceBase.Run(application);
            }
        }

        public IConfiguration Configuration
        {
            get { return applicationWrapper.Configuration; }
        }
    }
}
